﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace IFDB.Models.Foundation
{
    public abstract class BaseTable<T>
    {
        protected static IEnumerable<T> ProcessSearch(IEnumerable<T> localQuery, IEnumerable<T> dbQuery, bool allowMultiple = true)
        {
            switch (localQuery.Count())
            {
                case 0:
                    switch (dbQuery.Count())
                    {
                        case 0:
                            return null;

                        case 1:
                            return dbQuery;

                        default:
                            if (allowMultiple)
                                return dbQuery;
                            else
                                throw new Exception("Multiple records found");
                    }

                case 1:
                    return localQuery;

                default:
                    if (allowMultiple)
                        return localQuery;
                    else
                        throw new Exception("Multiple records found");
            }
        }
    }
}
